/*
 * @Descripttion: 
 * @Author: junhuafly
 * @Date: 2020-03-02 11:44:29
 * @LastEditors: junhuafly
 * @Email: junhuafly@163.com
 * @LastEditTime: 2020-04-21 10:21:26
 */
// pages/reply/index.js
import {
  noteAPI
} from '../../api/note'
import {
  returnStatus
} from '../../utils/util'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    replyId: 529,
    pageNum: 1,
    showReply: false,
    subReplyList: [],
    likeTops: [],
    like: 0,
    totalReply: 0,
    replyVal: '',
    subReply: {},
    requestObj: {},
    info: {}
  },

  onHideReplyMask() {
    this.setData({
      showReply: false
    })
  },
  onShowReplyMask({
    detail
  }) {
    this.setData({
      showReply: true,
      subReply: detail
    })
  },
  fetchData() {
    return new Promise((resolve, reject) => {
      // 加载评论
      noteAPI.querySubReply({
        replyId: this.data.replyId,
        pageNum: this.data.pageNum
      }).then(_ => {
        returnStatus(_).then(res => {
          this.setData({
            subReplyList: [...this.data.subReplyList, ...res.records],
            pageNum: ++this.data.pageNum
          })
          resolve(res.total)
        }).catch(res => {
          reject(false)
        })
      })
    })
  },
  
  toLike(){
    const {replyId} = this.data.info
    wx.navigateTo({
      url: `/pages/mine/attention/index?replyId=${replyId}`,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function ({id}) {
    var appInst =  getApp();
    const _this = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('commentCardInfo', function ({
      detail
    }) {
      _this.setData({
        info: detail,
        replyId: detail.replyId
      })

    appInst.setTitle(`${detail.subReplyCount}条回复`)
    })
    this.setData({
      requestObj: {
        fetchData(refresh) {
          refresh ? (
            _this.setData({
              pageNum: 1,
              subReplyList: []
            })) : ''
          return _this.fetchData()
        },
      }
    })

    const {
      replyId
    } = this.data
    // 点赞TOP
    noteAPI.queryReplyLikeTop({
      replyId
    }).then(async res => {
      const _ = await returnStatus(res)
      if (_) {
        this.setData({
          likeTops: _.records,
          like: _.total
        })
      }
    })
    this.fetchData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})